package com.hornsun.client.personalPage.api;

import com.hornsun.client.personalPage.dto.PersonalPageDTO;
import com.hornsun.client.personalPage.service.HealthBeansService;
import com.hornsun.repository.MemberCouponRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 编译中... on 2018/4/23.
 */
@RestController
@RequestMapping("/api/personalPage")
public class PersonalPageAPI {
    @Autowired
    private HealthBeansService healthBeansService;

    @Autowired
    private MemberCouponRepository memberCouponRepository;

    /**
     * 个人主页
     * @param httpRequest
     * @return coupon：优惠券数；healthBeans:健康豆数
     */
    @GetMapping("toPersonalPage")
    public ResponseEntity<PersonalPageDTO> toPersonalPage(HttpServletRequest httpRequest){
        String openid = httpRequest.getAttribute("openid").toString();
        int healthBeans = healthBeansService.queryHealthBeans(openid);
        int coupon = (int)memberCouponRepository.usableCouponCount(openid);
        PersonalPageDTO personalPageDTO = new PersonalPageDTO();
        personalPageDTO.setCoupon(coupon);
        personalPageDTO.setHealthBeans(healthBeans);
        return new ResponseEntity<PersonalPageDTO>(personalPageDTO, HttpStatus.OK);
    }

}
